package com.example.demo.service;

import com.example.demo.entity.Orders;
import com.example.demo.mapper.GoodsMapper;
import com.example.demo.mapper.OrdersMapper;
import com.example.demo.utils.CommonUtils;
import com.example.demo.vo.OrdersWithGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrderService {
    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    GoodsMapper goodsMapper;

    // 单表的增删改都可以直接用封装好的
    public List<Orders> getAllOrder(){
        return ordersMapper.selectAll();
    }

    public int addOrders(Orders orders){
        orders.setOrderTime(new Date());
        // 得到商品单价
        String code = orders.getGoodsCode();
        double price = goodsMapper.getPriceById(code);
        orders.setMoneyAmount(price * orders.getGoodsAmount());
        orders.setId(CommonUtils.getUUID());
        return ordersMapper.insertSelective(orders);
    }

    /**
     * 显示订单信息
     * @param id 订单ID
     * @return
     */
    public OrdersWithGoods showOrders(String id){
        return ordersMapper.getOrderInfoById(id);
    }
}
